<template>
    <view>
        <view class="goods-list">
            <view v-for="(item, index) in goodsList" :key="index" @click="goToDetail(item)">
                <!--  -->
                <my-goods :item="item"></my-goods>
            </view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            // 请求参数对象
            queryObj: {
                query: '',
                cid: '',
                pagenum: 1,
                pagesize: 10
            },
            // 商品列表
            goodsList: [],
            // 总数居条数
            total: 0,
            // 节流阀变量
            isloading: false
        };
    },
    onLoad(options) {
        this.queryObj.query = options.query || '';
        this.queryObj.cid = options.cid || '';
        this.getGoodsList();
    },
    methods: {
        // 获取商品列表
        async getGoodsList(cb) {
            // 为请求设置节流阀
            this.isloading = true;
            const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
            // console.log(res);
            if (res.meta.status !== 200) return uni.$showMsg();
            this.goodsList = [...this.goodsList, ...res.message.goods];
            this.isloading = false;
            this.total = res.message.total;
            cb && cb();
        },
        goToDetail(item) {
            uni.navigateTo({
                url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
            });
        }
    },
    // 上拉触底
    onReachBottom() {
        if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕');
        if (this.isloading) return;
        // 页码值 + 1
        this.queryObj.pagenum++;
        this.getGoodsList();
    },
    //下拉
    onPullDownRefresh() {
        //
        this.queryObj.pagenum = 1;
        this.total = 0;
        this.isloading = false;
        this.goodsList = [];
        //
        this.getGoodsList(() => uni.stopPullDownRefresh());
    }
};
</script>

<style lang="scss"></style>
